package com.example.administrator.lazero_xposed;

//package ai.lazero.lazero;

//import android.content.pm.ApplicationInfo;

import de.robv.android.xposed.IXposedHookLoadPackage;
//import com.android.server.pm.Settings;
//import de.robv.android.xposed.IXposedHookZygoteInit;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
//import de.robv.android.xposed.mods.appsettings.XposedMod;

public class HookTest_v3 implements IXposedHookLoadPackage {
    //    public boolean isExecuted = false;
//    public int counter = 3;
    // it is dead.
    public static final int sysuid = 10173;
    // root uid is 0.
    public static final int flag_system = 1 << 0;
    public static final int priv_flag = 1 << 3;

    //    final Class <?> test = XposedHelpers.findClass("com.android.server.pm.PackageManagerService",XC_LoadPackage.LoadPackageParam.class.getClassLoader());
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        Class clazz = loadPackageParam.classLoader.loadClass(
                "com.android.server.pm.Settings");
// hooking all methods will result in disaster.
//            XposedHelpers.find
        XposedHelpers.findAndHookMethod(clazz, "addSharedUserLPw", String.class, int.class, int.class, int.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                XposedBridge.log("Permission UID method has Hooked! " + (String) param.args[0]);
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//param.method.
//                    if (!isExecuted) {
//                        if (counter > 0) {
//                            counter -= 1;
//                        } else {
                try {
                    if (param.args[0].equals("android.uid.system")) {
                        try {
                            XposedBridge.log("PM has Hooked!");
                            XposedHelpers.callMethod(param.thisObject, "addSharedUserLPw", "ai.lazero.lazero", sysuid, flag_system, priv_flag);
                            XposedBridge.log("Somehow Executed.");
                        } catch (Exception e) {
                            XposedBridge.log(e.toString());
                        } finally {
//                                isExecuted = true;
                            XposedBridge.log("Try Block Executed.");
                        }
                    }
                } catch (Exception e) {
                    XposedBridge.log(e.toString());
                }
//                        }
//                    }
                // hook method and then change by the content.
                // restore permissions and do not modify the result.
//                    param.setResult("你已被劫持");
            }

        });

    }

}
